package com.qf.service.impl;

import com.qf.service.UserService;
import com.qf.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.qf.pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserServiceImpl implements UserService {

//    private UserMapper userMapper = new UserMapperImpl();

    @Override
    public List<User> findAll() throws IOException {
        //遵循mybatis规则，获取mapper对象
        //将核心配置文件加载进内存，形成输入流(Resources内部就是通过类加载器完成)
        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //通过builder对象获取SqlSessionFactory
        SqlSessionFactory factory = builder.build(is);

        //开启一个SqlSession对象（对Connection的封装）
        SqlSession sqlSession = factory.openSession();

        //获取到一个UserMapper的对象(动态代理)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.findAll();

        //归还连接
        sqlSession.close();
        return users;
    }

    @Override
    public User findById(Integer id) throws IOException {
        //遵循mybatis规则，获取mapper对象
        //将核心配置文件加载进内存，形成输入流(Resources内部就是通过类加载器完成)
        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //通过builder对象获取SqlSessionFactory
        SqlSessionFactory factory = builder.build(is);

        //开启一个SqlSession对象（对Connection的封装）
        SqlSession sqlSession = factory.openSession();

        //获取到一个UserMapper的对象(动态代理)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = userMapper.findById(id);

        //归还连接
        sqlSession.close();
        return user;
    }

    @Override
    public int insert(User user) throws IOException {
        //遵循mybatis规则，获取mapper对象
        //将核心配置文件加载进内存，形成输入流(Resources内部就是通过类加载器完成)
        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //通过builder对象获取SqlSessionFactory
        SqlSessionFactory factory = builder.build(is);

        //开启一个SqlSession对象（对Connection的封装）

        //openSession() -- 获取的connection是非自动提交的，执行完业务后，需要手动执行commit操作 （做事务操作）
        //openSession(true) -- 获取的connection是自动提交的，执行完业务后,事务自动提交

//        SqlSession sqlSession = factory.openSession();
        SqlSession sqlSession = factory.openSession(true);

        //获取到一个UserMapper的对象(动态代理)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        int count = userMapper.insert(user);

//        //提交事务
//        sqlSession.commit();

        //归还连接
        sqlSession.close();
        return count;
    }

    @Override
    public int update(User user) throws IOException {
        //遵循mybatis规则，获取mapper对象
        //将核心配置文件加载进内存，形成输入流(Resources内部就是通过类加载器完成)
        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //通过builder对象获取SqlSessionFactory
        SqlSessionFactory factory = builder.build(is);

        //开启一个SqlSession对象（对Connection的封装）

        //openSession() -- 获取的connection是非自动提交的，执行完业务后，需要手动执行commit操作 （做事务操作）
        //openSession(true) -- 获取的connection是自动提交的，执行完业务后,事务自动提交

//        SqlSession sqlSession = factory.openSession();
        SqlSession sqlSession = factory.openSession(true);

        //获取到一个UserMapper的对象(动态代理)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        int count = userMapper.update(user);

//        //提交事务
//        sqlSession.commit();

        //归还连接
        sqlSession.close();
        return count;
    }

    @Override
    public int delete(Integer id) throws IOException {
        //遵循mybatis规则，获取mapper对象
        //将核心配置文件加载进内存，形成输入流(Resources内部就是通过类加载器完成)
        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //通过builder对象获取SqlSessionFactory
        SqlSessionFactory factory = builder.build(is);

        //开启一个SqlSession对象（对Connection的封装）

        //openSession() -- 获取的connection是非自动提交的，执行完业务后，需要手动执行commit操作 （做事务操作）
        //openSession(true) -- 获取的connection是自动提交的，执行完业务后,事务自动提交

//        SqlSession sqlSession = factory.openSession();
        SqlSession sqlSession = factory.openSession(true);

        //获取到一个UserMapper的对象(动态代理)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        int count = userMapper.delete(id);

//        //提交事务
//        sqlSession.commit();

        //归还连接
        sqlSession.close();
        return count;
    }

    @Override
    public List<User> findByConditions(User user) throws IOException {
        //遵循mybatis规则，获取mapper对象
        //将核心配置文件加载进内存，形成输入流(Resources内部就是通过类加载器完成)
        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //通过builder对象获取SqlSessionFactory
        SqlSessionFactory factory = builder.build(is);

        //开启一个SqlSession对象（对Connection的封装）
        SqlSession sqlSession = factory.openSession();

        //获取到一个UserMapper的对象(动态代理)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.findByConditions(user);

        //归还连接
        sqlSession.close();
        return users;
    }

    @Override
    public List<User> findByNameAndAge(String name, Integer age) throws IOException {
        //遵循mybatis规则，获取mapper对象
        //将核心配置文件加载进内存，形成输入流(Resources内部就是通过类加载器完成)
        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //通过builder对象获取SqlSessionFactory
        SqlSessionFactory factory = builder.build(is);

        //开启一个SqlSession对象（对Connection的封装）
        SqlSession sqlSession = factory.openSession();

        //获取到一个UserMapper的对象(动态代理)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.findByNameAndAge(name,age);

        //归还连接
        sqlSession.close();
        return users;
    }

    @Override
    public List<User> findByIds(List<Integer> ids) throws IOException {
        //遵循mybatis规则，获取mapper对象
        //将核心配置文件加载进内存，形成输入流(Resources内部就是通过类加载器完成)
        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //通过builder对象获取SqlSessionFactory
        SqlSessionFactory factory = builder.build(is);

        //开启一个SqlSession对象（对Connection的封装）
        SqlSession sqlSession = factory.openSession();

        //获取到一个UserMapper的对象(动态代理)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.findByIds(ids);

        //归还连接
        sqlSession.close();
        return users;
    }

    @Override
    public List<User> findByIds2(Integer... ids) throws IOException {
        //遵循mybatis规则，获取mapper对象
        //将核心配置文件加载进内存，形成输入流(Resources内部就是通过类加载器完成)
        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //通过builder对象获取SqlSessionFactory
        SqlSessionFactory factory = builder.build(is);

        //开启一个SqlSession对象（对Connection的封装）
        SqlSession sqlSession = factory.openSession();

        //获取到一个UserMapper的对象(动态代理)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.findByIds2(ids);

        //归还连接
        sqlSession.close();
        return users;
    }
}
